package com.yycx.module.bus.provider.handler.haiying.tasktodo.order;

import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.client.enums.HySaleTypeEnum;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.file.provider.service.SysFileService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysBankService;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的已办订单任务列表
 *
 * @author zyf
 */
@Component
public class HyOrderTaskTab2ListHandler implements PageInterceptor<HyOrder> {

    @Resource
    private BaseUserService userService;

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private FlowableHelp flowableHelp;

    @Override
    public void prepare(CriteriaQuery<HyOrder> cq, PageParams pageParams, EntityMap params) {
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String customId = cq.getParams("customId");
        String orderNo = cq.getParams("orderNo");
        cq.eq(HyOrder.class, "deptId");
        cq.eq(HyOrder.class, "companyId");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("sendDate", beginDate);
            cq.le("sendDate", endDate);
        }
        cq.eq(FlymeUtils.isNotEmpty(customId), "customId", customId);
        cq.in(true, "orderState", 1);
        cq.eq(true, "orderType", HySaleTypeEnum.CHA.getCode());
        String userName = OpenHelper.getUserName();
        //已审批中有我的记录
        if (!userName.contains("admin")) {
            Long userId = OpenHelper.getUserId();
            cq.apply("'" + userId + "' in(select USER_ID_ from act_hi_comment c where c.PROC_INST_ID_=ho.processInstanceId and c.TYPE_='WC' and c.ACTION_ ='AddComment' and c.USER_ID_='" + userId + "')");
        }
        //销售角色
        Boolean xs = OpenHelper.hasRole("ROLE_XS_BZ", "ROLE_XS_FHY");
        if (!xs || userName.contains("admin")) {
            Long companyId = OpenHelper.getCompanyId();
            cq.eq("ho.companyId", companyId);
        }
        cq.orderByDesc("createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<HyOrder> cq, List<EntityMap> result, EntityMap extra) {

        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Long userId = entityMap.getLong("userId");
            String assignee = entityMap.get("assignee");
            EntityMap taskMap = flowableHelp.getTaskInfo(processInstanceId);
            if (FlymeUtils.isNotEmpty(taskMap)) {
                entityMap.putAll(taskMap);
            }
            Integer orderState = entityMap.getInt("orderState");
            Long companyId = entityMap.getLong("companyId");
            SysCompany company = companyService.getById(companyId);
            entityMap.put("companyName", company.getCompanyName());
            BaseUser orderUser = userService.getById(userId);
            if (FlymeUtils.isNotEmpty(orderUser)) {
                entityMap.put("nickName", orderUser.getNickName());
            }
            if (orderState.equals(2)) {
                entityMap.put("taskName", "已审批");
            }

        }
        return result;
    }

}
